package com.hc.ssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.fastjson.JSON;
import com.hc.ssm.services.BlogServices;

@Controller
@RequestMapping(value = "blog")
public class BlogController {
	@Autowired
	private BlogServices blogServices;

	@RequestMapping(value = "findBlog")
	public String findBlog(Map<String, Object> map, @RequestParam String id,
			@RequestParam int pagesize, @RequestParam int indexpage) {
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("id", id);
		map1.put("indexpage", pagesize * (indexpage - 1));
		map1.put("pagesize", pagesize);
		List<Map<String, Object>> list = blogServices.findBlog(map1);
		Map<String, Object> map2 = blogServices.countBlog(map1);

		long tableSize = (Long) map2.get("count");
		System.out.println(JSON.toJSONString(list));

		long pageNum = tableSize % pagesize == 0 ? tableSize / pagesize
				: (tableSize / pagesize + 1);
		map.put("blogList", list);
		map.put("pageNum", pageNum);
		map.put("tableSize", tableSize);
		map.put("indexpage", indexpage);
		map.put("pagesize", pagesize);

		return "index";
	}

	@RequestMapping(value = "addBlog")
	public String addBlog(@RequestParam int id, @RequestParam String content) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("content", content);
		
		blogServices.addBlog(map);
		return "redirect:/blog/findBlog?id=" + id + "&pagesize=5&indexpage=1";
	}
	
	
	@RequestMapping(value = "deleteBlog")
	public String deleteBlog(@RequestParam int id,@RequestParam int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		
		blogServices.deleteBlog(map);
		return "redirect:/blog/findBlog?id=" + userId + "&pagesize=5&indexpage=1";
	}
	


}
